package client;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.Socket;
import java.util.ArrayList;

import client.gui.TrackPanel;

public class MusicClient extends Client implements Runnable {
	/**
	 * The List of Services resolved
	 */
	private String artist;
	private String album;
	private String track;
	private TrackPanel panel;
	public MusicClient(Service s) {
		super(s);
	}

	public void run() {
		byte[] buf = new byte[256];
		DatagramPacket packet = new DatagramPacket(buf, buf.length);
		try {
			Service thisServer = getServer();
			Socket socket = new Socket(thisServer.getIpAddress(), thisServer.getPort());
			PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
			out.println("playTrack" + "," + artist + "," + album +","+ track+",");
			int port = socket.getLocalPort();
			socket.close();
			DatagramSocket socketD = new DatagramSocket(port);
		    packet = new DatagramPacket(buf, buf.length);
		    String received ="";
		    while(!received.equals("The Song is now Finished:")){
		    socketD.receive(packet);
		    panel.incrementProgress();
		    received = new String(packet.getData(), 0, packet.getLength());
		    }
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	public ArrayList<String> getArtistList() {
		ArrayList<String> artist = null;
		try {
			Service thisServer = getServer();
			Socket socket = new Socket(thisServer.getIpAddress(), thisServer.getPort());
			PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
			out.println("getArtistList");
			System.out.println("Here");
			out.flush();
			ObjectInputStream oIn = new ObjectInputStream(socket.getInputStream());
			artist = (ArrayList<String>) oIn.readObject();
			out.close();
			oIn.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return artist;
	}

	@SuppressWarnings("unchecked")
    public ArrayList<String> getAlbumList(String Artist){
		ArrayList<String> albums = null;
		try {
			Service thisServer = getServer();
			Socket socket = new Socket(thisServer.getIpAddress(), thisServer.getPort());
			PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
			out.println("getAlbumList" + "," + Artist+",");
			out.flush();
			ObjectInputStream oIn = new ObjectInputStream(socket.getInputStream());
			albums = (ArrayList<String>) oIn.readObject();
			out.close();
			oIn.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return albums;
	}

	@SuppressWarnings("unchecked")
    public ArrayList<String> getTrackList(String Artist, String Album){
		ArrayList<String> albums = null;
		try {
			Service thisServer = getServer();
			Socket socket = new Socket(thisServer.getIpAddress(), thisServer.getPort());
			PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
			out.println("getTrackList" + "," + Artist + "," + Album+ ",");
			out.flush();
			ObjectInputStream oIn = new ObjectInputStream(socket.getInputStream());
			albums = (ArrayList<String>) oIn.readObject();
			out.close();
			oIn.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return albums;
	}

	@SuppressWarnings("unchecked")
	public ArrayList<String> searchArtist(String Artist){
		ArrayList<String> artists = null;
		try {
			Service thisServer = getServer();
			Socket socket = new Socket(thisServer.getIpAddress(), thisServer.getPort());
			PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
			out.println("searchArtist" + "," + Artist+",");
			out.flush();
			ObjectInputStream oIn = new ObjectInputStream(socket.getInputStream());
			artists = (ArrayList<String>) oIn.readObject();
			out.close();
			oIn.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return artists;

	}
	
	public void playTrack(String Artist, String Album, String Track, TrackPanel t){
		track = Track;
		artist = Artist;
		album = Album;
		panel = t;
		(new Thread(this)).start();
	}

}
